/**
 * 仓库
 *
 * @author sxf
 * @email sxf02615@163.com
 * @date 2025/1/15
 */
package com.sxf.crm.repository;


import com.sxf.crm.entity.User;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Optional;

@Repository
public interface UserRepository extends JpaRepository<User, Long> {
    Optional<User> findByUsername(String username);
    
    @Query("SELECT c FROM User c WHERE " +
    "(:username IS NULL OR :username = '' OR c.username LIKE %:username%) AND " +
    "(:realName IS NULL OR :realName = '' OR c.realName LIKE %:realName%) AND " +
    "(:status IS NULL OR (:status = 0 AND (c.status IS NULL OR c.status = 0)) OR (:status = 1 AND c.status = 1) )")
    Page<User> findByUsernameContainingAndRealNameContainingAndStatus(
        @Param("username") String username, 
        @Param("realName") String realName, 
        @Param("status") Integer status, 
        Pageable pageable
    );

    List<User> findByDepartmentId(Long departmentId);

    List<User> findByDepartmentIdIn(List<Long> departmentIds);
} 